package com.luobd.bpm.config;

import com.google.common.collect.Lists;
import com.luobd.bpm.activiti.CustomExecutionEntityManagerImpl;
import com.luobd.bpm.activiti.CustomProcessInstanceHelper;
import com.luobd.bpm.config.listener.MyActivitiEnvenListener;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.persistence.entity.data.ExecutionDataManager;
import org.activiti.engine.impl.persistence.entity.data.impl.MybatisExecutionDataManager;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.activiti.spring.boot.ProcessEngineConfigurationConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
@Slf4j
public class ActivitiConfiguration implements ProcessEngineConfigurationConfigurer {







    @Override
    public void configure(SpringProcessEngineConfiguration configuration) {
        addCustomEventListener(configuration);
        configuration.setProcessInstanceHelper(new CustomProcessInstanceHelper());
        setCustomExecutionEntityManager(configuration);

    }





    private void addCustomEventListener(SpringProcessEngineConfiguration configuration) {
        log.info("配置Activti事件监听器>>>>>>>");
        // 配置事件监听器
        List<ActivitiEventListener> eventListeners = configuration.getEventListeners();
        if (eventListeners == null) {
            eventListeners = Lists.newArrayList();
        }
        eventListeners.add(new MyActivitiEnvenListener());
        configuration.setEventListeners(eventListeners);
    }


    private void setCustomExecutionEntityManager(SpringProcessEngineConfiguration configuration) {
        MybatisExecutionDataManager mybatisExecutionDataManager = new MybatisExecutionDataManager(configuration);
        ProcessEngineConfigurationImpl processEngineConfiguration = configuration.getProcessEngineConfiguration();
        CustomExecutionEntityManagerImpl manager = new CustomExecutionEntityManagerImpl(processEngineConfiguration, mybatisExecutionDataManager);
        configuration.setExecutionEntityManager(manager);
        configuration.setExecutionDataManager(mybatisExecutionDataManager);
    }


}
